package cn.hpclub.server.model;/**
                                * Created by zenghl on 2016/9/19.
                                */

import java.io.Serializable;
import java.util.List;

import cn.hpclub.server.util.QueryHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.util.InterfaceUtil;

/**
 * @FileName CommunicationFee
 * @Author zhl
 * @Description 资费办理DB实体类
 * @Date 2016-09-19 10:24
 * @CopyRight ZTE Corporation
 */
@TableBind(tableName = "tariff")
public class Tariff extends Model<Tariff> implements Serializable{
    private static final Logger logger            = LoggerFactory.getLogger(Tariff.class);
    private static final long   serialVersionUID  = 5103397504836779527L;
    public static final Tariff  dao               = new Tariff();

    /**
     * 主键Id
     */
    public static String        ID                = "id";
    /**
     * 资费类型：0主资费，1附件资费，2营销活动，3营销活动（短信）
     */
    public static String        TYPE              = "type";
    /**
     * 资费名称
     */
    public static String        NAME              = "name";
    /**
     * 资费/短信代码
     */
    public static String        CODE              = "code";
    /**
     * 资费详情
     */
    public static String        DETAIL            = "detail";
    /**
     * 开始时间
     */
    public static String        START_DATE        = "start_date";
    /**
     * 结束时间
     */
    public static String        END_DATE          = "end_date";
    /**
     * 排序字段
     */
    public static String        SORT              = "sort";
    /**
     * 资费创建时间
     */
    public static String        CREATE_TIME       = "create_time";
    /**
     * 资费创建人
     */
    public static String        CREATE_BY         = "create_by";

    /**
     * 机构Id
     */
    public static String        ORG_ID            = "org_id";
    /**
     * 最近一次修改时间
     */
    public static String        LAST_UPDATED_TIME = "last_updated_time";
    /**
     * 最近一次被修改的用户ID
     */
    public static String        LAST_UPDATED_BY   = "last_updated_by";
    /**
     * 1:启用 0：禁用。默认启用
     */
    public static String        ENABLED           = "enabled";
    /**
     * 软删除标志位(0=有效，1=删除)
     */
    public static String        IS_DELETED        = "is_deleted";

    /**
     * 是否活动业务(0=不是，1=是)
     */
    public static String        IS_ACTIVITY       = "is_activity";
    /**
     * 活动名称
     */
    public static String        ACTIVITY_NAME     = "activity_name";
    /**
     * 资费详情
     */
    public static String        DESCRIPTION       = "description";
    /**
     * 是否属地化1为属地化授权，0为统一授权
     */
    public static String        LOCALIZED       = "localized";

    /**
     * @Title: getTariffByTariffClassId
     * @Description:根据资费大类ID获取资费小类数据，业务权限配置（资费类）和泛店员业务权限配置共用
     * @param tariffClassId：资费大类ID
     * @return List<Record>
     */
    public List<Record> getTariffByTariffClassId(int tariffClassId){
        int orgId = InterfaceUtil.getOrgId();
        int orgTopId = InterfaceUtil.getIdTop();
        logger.info("orgId = " + orgId + ", orgTopId = " + orgTopId);
        // String orgSql = InterfaceUtil.getChlidOrgIdSql(orgId,
        // Constant.MAX_ORG_LEVEL, true);
        /*
         * if(StrKit.notBlank(orgSql)){ sql += " AND t.org_id IN " + orgSql; }
         */

        //String sql = "SELECT t.id,t.name,t.type,t.code FROM tariff t LEFT JOIN tariff_class_relation tcr ON t.id = tcr.tariff_id LEFT JOIN tariff_class tc ON tcr.tariff_class_id = tc.id WHERE tc.id = ? AND t.is_deleted=0 AND t.enabled=1 AND (t.start_date <= CURDATE() AND t.end_date >= CURDATE())";
        //sql += " AND t.org_id=" + orgId;// 熊诗言：只取当前机构的
        QueryHelper helper = new QueryHelper("SELECT t.id,t.name,t.type,t.code","tariff t " +
                "LEFT JOIN tariff_class_relation tcr ON t.id = tcr.tariff_id " +
                "LEFT JOIN tariff_class tc ON tcr.tariff_class_id = tc.id " +
                "LEFT JOIN localized_config lc ON (lc.business=0 AND lc.business_id=t.id)");
        helper.addCondition("tc.id = ?",tariffClassId);
        helper.addCondition("t.is_deleted=0");
        helper.addCondition("t.enabled=1");
        helper.addCondition("t.start_date <= CURDATE()");
        helper.addCondition("t.end_date >= CURDATE()");
        //对于省级管理员，取自己的并且是统一授权的
        //对于地市级管理员，取自己的或者属地化给自己的
        helper.addCondition(orgId==orgTopId,"t.org_id=? AND t.localized=?",orgId,LocalizedConfig.LOCALIZED_UNIFIED);//省级：自己并且是统一授权的
        helper.addCondition(orgId!=orgTopId,"(t.org_id=? OR (t.org_id=? AND t.localized=? AND lc.localized_city_id=?))",orgId,orgTopId,LocalizedConfig.LOCALIZED_LOCALIZED,orgId);//地市级：取自己的或者属地化给自己的

        return Db.find(helper.getSelect()+helper.getSqlExceptSelect());
    }

    /**
     * @Title: getSqlExceptSelect
     * @Description:泛店员获取已授权列表时组装sql脚本
     * @return String
     */
    public String getSqlExceptSelect(){
        return "tariff bus_temp ON bac.config_business_id = bus_temp.id WHERE bus_temp.enabled = 1 AND bus_temp.is_deleted = 0 AND bus_temp.end_date >= CURDATE()";
    }

}
